﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.WinForms;
using RM2012Server.Controller;
using RM2012Server.Model;
using RM2012Server.Business;

namespace RM2012Server.View
{
    public partial class frmBooking : Office2007Form
    {
        #region Fields
        CustomerCtrl m_CustomerCtrl = new CustomerCtrl();
        BookingCtrl m_BookingCtrl = new BookingCtrl();
        TablesCtrl m_TablesCtrl = new TablesCtrl();
        public static bool isNew = false;
        #endregion

        #region Constructor
        public frmBooking()
        {
            InitializeComponent();
        }
        #endregion

        #region Load
        private void frmBooking_Load(object sender, EventArgs e)
        {
            isNew = false;
            m_TablesCtrl.LoadToComboBox(cmbTablesId);
            m_CustomerCtrl.LoadToComboBox(cmbCusId);
            m_CustomerCtrl.LoadToDataGridViewComboBoxColumn(colCusId);
            m_TablesCtrl.LoadToDataGridViewComboBoxColumn(colTablesId);
            m_BookingCtrl.LoadToDataGridViewComboBoxColumnState(colState);
            m_BookingCtrl.Show(dgvBooking, bindingNavigatorBooking, txtBookId, cmbCusId, txtBookTime, dtBookTime, txtTablesId, chkHaveTables, cmbTablesId, iiQuantity, cmbState, txtRemark);
            SaveItem.Enabled = false;
            if (dgvBooking.Rows.Count > 0)
            {
                if ((dgvBooking.Rows[0].Cells["colTablesId"].Value != null) && (!dgvBooking.Rows[0].Cells["colTablesId"].Value.ToString().Equals("")))
                {
                    chkHaveTables.Checked = true;
                }
            }
        }
        #endregion

        #region BindingNavigator Item Event Click
        private void AddNewItem_Click(object sender, EventArgs e)
        {
            isNew = true;
            if (dgvBooking.Rows.Count == 0)
            {
                DeleteItem.Enabled = false;
            }
            DataRow m_Row = m_BookingCtrl.AddNewRow();
            String dateTimeNow = String.Empty;
            dateTimeNow = DateTime.Now.Month.ToString() + "/" + DateTime.Now.Day.ToString() + "/" + DateTime.Now.Year.ToString() + " " + DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString();
            m_Row["BookId"] = GetID.Booking(dgvBooking);
            m_Row["CusId"] = "";
            m_Row["TablesId"] = "";
            m_Row["Quantity"] = 1;
            m_Row["BookTime"] = dateTimeNow;
            m_Row["State"] = 0;
            m_Row["Remark"] = "";
            m_BookingCtrl.AddNewBooking(m_Row);
            bindingNavigatorBooking.BindingSource.MoveLast();
            SaveItem.Enabled = true;
        }

        private void RefreshItem_Click(object sender, EventArgs e)
        {
            isNew = false;
            m_BookingCtrl.Show(dgvBooking, bindingNavigatorBooking, txtBookId, cmbCusId, txtBookTime, dtBookTime, txtTablesId, chkHaveTables, cmbTablesId, iiQuantity, cmbState, txtRemark);
            SaveItem.Enabled = false;
        }

        private void DeleteItem_Click(object sender, EventArgs e)
        {
            isNew = false;
            if (dgvBooking.Rows.Count == 0)
            {
                DeleteItem.Enabled = false;
            }
            else if (dgvBooking.SelectedRows.Count > 0)
            {
                if (MessageBoxEx.Show("Bạn có chắc chắn xóa?", "XÓA", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    foreach (DataGridViewRow row in dgvBooking.SelectedRows)
                    {
                        bindingNavigatorBooking.BindingSource.RemoveAt(row.Index);
                    }
                    SaveItem.Enabled = true;
                }
            }
        }

        private void SaveItem_Click(object sender, EventArgs e)
        {
            isNew = false;
            bindingNavigatorPositionItem.Focus();
            bool check = true;
            foreach (DataGridViewRow row in dgvBooking.Rows)
            {
                if (row.Cells["colBookId"].Value.ToString() == "" || row.Cells["colCusId"].Value.ToString() == "")
                {
                    check = false;
                }
            }
            if (check)
            {
                if (!m_BookingCtrl.SaveData())
                {
                    MessageBoxEx.Show("Không thể thực hiện.", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    SaveItem.Enabled = false;
                }
            }
            else
            {
                MessageBoxEx.Show("Dữ liệu không hợp lệ !", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void ExitItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region Button Click
        private void btnDongY_Click(object sender, EventArgs e)
        {
            BookingInfo info = new BookingInfo();
            info.BookId = txtBookId.Text;
            info.CusId = cmbCusId.SelectedValue.ToString();
            if ((!cmbTablesId.Text.Equals("")) && (cmbTablesId.Text != null))
            {
                info.TablesId = cmbTablesId.SelectedValue.ToString();
            } 
            else
            {
                info.TablesId = String.Empty;
            }
            info.BookTime = dtBookTime.Value;            
            info.Quantity = iiQuantity.Value;
            info.State = cmbState.SelectedIndex;
            info.Remark = txtRemark.Text;

            if (isNew == true)
            {
                m_BookingCtrl.Add(info);
            }
            else
            {
                m_BookingCtrl.Update(info);
            }
            m_BookingCtrl.Show(dgvBooking, bindingNavigatorBooking, txtBookId, cmbCusId, txtBookTime, dtBookTime, txtTablesId, chkHaveTables, cmbTablesId, iiQuantity, cmbState, txtRemark);
            SaveItem.Enabled = false;
            isNew = false;
        }

        private void btnLamLai_Click(object sender, EventArgs e)
        {
            isNew = false;
            m_BookingCtrl.Show(dgvBooking, bindingNavigatorBooking, txtBookId, cmbCusId, txtBookTime, dtBookTime, txtTablesId, chkHaveTables, cmbTablesId, iiQuantity, cmbState, txtRemark);
            SaveItem.Enabled = false;
        }
        #endregion

        #region DataGridView Event
        private void dgvBooking_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            string strTablesId = dgvBooking.Rows[e.RowIndex].Cells["colTablesId"].Value.ToString();
            if ((!strTablesId.Equals("")) && (!(strTablesId == null)))
            {
                chkHaveTables.Checked = true;
            }
            else
            {
                chkHaveTables.Checked = false;
            }
        }

        private void dgvBooking_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            SaveItem.Enabled = true;
        }

        private void dgvBooking_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            MessageBoxEx.Show("Dữ liệu không hợp lệ !", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        #endregion

        #region Searching
        private void btnTimKiem_Click(object sender, EventArgs e)
        {

        }
        #endregion

        #region Context Menu Click Event
        private void AddNewItemContext_Click(object sender, EventArgs e)
        {
            AddNewItem_Click(sender, e);
        }

        private void RefreshItemContext_Click(object sender, EventArgs e)
        {
            RefreshItem_Click(sender, e);
        }

        private void DeleteItemContext_Click(object sender, EventArgs e)
        {
            DeleteItem_Click(sender, e);
        }

        private void SaveItemContext_Click(object sender, EventArgs e)
        {
            SaveItem_Click(sender, e);
        }

        private void ExitItemContext_Click(object sender, EventArgs e)
        {
            ExitItem_Click(sender, e);
        }
        #endregion
    }
}
